package com.sgcc.sx.pmos.e_sx_mall.fragment;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.sx.pmos.arouter.mall.MallArouterPath;
import com.sgcc.sx.pmos.common.base.MvpBaseRecycleFragment;
import com.sgcc.sx.pmos.common.bean.EventCenter;
import com.sgcc.sx.pmos.common.bean.PackageBean;
import com.sgcc.sx.pmos.common.utils.ArguConstant;
import com.sgcc.sx.pmos.common.utils.ListPageUtil;
import com.sgcc.sx.pmos.e_sx_mall.adapter.AllGoodsAdapter;
import com.sgcc.sx.pmos.e_sx_mall.bean.MallGoodsBean;
import com.sgcc.sx.pmos.e_sx_mall.contract.AllGoodsContract;
import com.sgcc.sx.pmos.e_sx_mall.presenter.AllGoodsPresenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 商品
 */
public class AllGoodsFragment extends MvpBaseRecycleFragment<PackageBean, AllGoodsPresenter> implements AllGoodsContract.View {

    private AllGoodsAdapter mAllGoodsAdapter;

    private String membersSdId;

    @Override
    protected AllGoodsPresenter createPresenter() {
        return new AllGoodsPresenter();
    }

    @Override
    protected void initViews() {
        super.initViews();
        membersSdId = getArguments().getString("membersSdId");
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mAllGoodsAdapter = new AllGoodsAdapter(mContext);
        mAllGoodsAdapter.setItemClickListener(new AllGoodsAdapter.OnItemClickListener() {

            @Override
            public void OnAddCollectionListener(int position, PackageBean s) {

            }

            @Override
            public void OnCancleCollectionListener(int position, PackageBean s) {

            }

            @Override
            public void OnItemsClickListener(int position, PackageBean bean) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_PACKAGEDETAILS)
                        .withString("combName", bean.getCombCode())
                        .withBoolean("isBlue", "1".equals(bean.getpNum()) && ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) &&
                                "0".equals(bean.getMainItemType()) &&
                                ("0".equals(bean.getMainDifType()) || TextUtils.isEmpty(bean.getMainDifType())))

                        .navigation();
            }
        });
        return mAllGoodsAdapter;
    }

    int mListSize;

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mListSize = listSize;
        mPresenter.getGoodsList(membersSdId, listSize, isloading);
    }


    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == ArguConstant.REFRESH_GOODS_LIST) {
            refresh(true);
        }
    }

    @Override
    public void queryGoodsListSuccess(MallGoodsBean response) {
        if (response != null) {
            if (response.getCode() == 200) {
                if (response.getData() != null) {
                    loadSuccess(ListPageUtil.isHasNextPage(response.getData().getTotal(), mListSize), response.getData().getData());
                }
            } else {
                loadFalied();
            }
        }
    }

    @Override
    public void queryGoodsListFailed() {
        loadFalied();
    }
}
